add Ace editor for editing JavaScript in credentials

Andrew Cantino %!s(int64=10) %!d(string=hace) años
padre
commit
c043a40868

+ 1 - 0
Gemfile

@@ -24,6 +24,7 @@ gem 'coffee-rails', '~> 3.2.1'
24 24
 gem 'uglifier', '>= 1.0.3'
25 25
 gem 'select2-rails'
26 26
 gem 'jquery-rails'
27
+gem 'ace-rails-ap'
27 28
 
28 29
 gem 'geokit-rails3'
29 30
 gem 'kramdown'

+ 2 - 0
Gemfile.lock

@@ -1,6 +1,7 @@
1 1
 GEM
2 2
   remote: https://rubygems.org/
3 3
   specs:
4
+    ace-rails-ap (2.0.1)
4 5
     actionmailer (3.2.13)
5 6
       actionpack (= 3.2.13)
6 7
       mail (~> 2.5.3)
@@ -272,6 +273,7 @@ PLATFORMS
272 273
   ruby
273 274
 
274 275
 DEPENDENCIES
276
+  ace-rails-ap
275 277
   better_errors
276 278
   binding_of_caller
277 279
   bootstrap-kaminari-views

+ 27 - 0
app/assets/javascripts/user_credentials.js.coffee

@@ -0,0 +1,27 @@
1
+#= require ace/ace
2
+#= require ace/mode-javascript.js
3
+#= require ace/mode-markdown.js
4
+#= require_self
5
+
6
+$ ->
7
+  editor = ace.edit("ace-credential-value")
8
+  editor.getSession().setTabSize(2)
9
+  editor.getSession().setUseSoftTabs(true)
10
+  editor.getSession().setUseWrapMode(false)
11
+  editor.setTheme("ace/theme/chrome")
12
+
13
+  setMode = ->
14
+    mode = $("#user_credential_mode").val()
15
+    if mode == 'java_script'
16
+      editor.getSession().setMode("ace/mode/javascript")
17
+    else
18
+      editor.getSession().setMode("ace/mode/text")
19
+
20
+  setMode()
21
+  $("#user_credential_mode").on 'change', setMode
22
+
23
+  $textarea = $('#user_credential_credential_value').hide()
24
+  editor.getSession().setValue($textarea.val())
25
+
26
+  $textarea.closest('form').on 'submit', ->
27
+    $textarea.val(editor.getSession().getValue())

+ 8 - 0
app/assets/stylesheets/application.css.scss.erb

@@ -126,3 +126,11 @@ span.not-applicable:after {
126 126
 #show-tabs li a.recent-errors {
127 127
   font-weight: bold;
128 128
 }
129
+
130
+// Credentials
131
+
132
+#ace-credential-value {
133
+  position: relative;
134
+  width: 940px;
135
+  height: 400px;
136
+}

+ 9 - 1
app/models/user_credential.rb

@@ -1,13 +1,17 @@
1 1
 class UserCredential < ActiveRecord::Base
2
-  attr_accessible :credential_name, :credential_value
2
+  MODES = %w[text java_script]
3
+
4
+  attr_accessible :credential_name, :credential_value, :mode
3 5
 
4 6
   belongs_to :user
5 7
 
6 8
   validates_presence_of :credential_name
7 9
   validates_presence_of :credential_value
10
+  validates_inclusion_of :mode, :in => MODES
8 11
   validates_presence_of :user_id
9 12
   validates_uniqueness_of :credential_name, :scope => :user_id
10 13
 
14
+  before_validation :default_mode_to_text
11 15
   before_save :trim_fields
12 16
 
13 17
   protected
@@ -16,4 +20,8 @@ class UserCredential < ActiveRecord::Base
16 20
     credential_name.strip!
17 21
     credential_value.strip!
18 22
   end
23
+
24
+  def default_mode_to_text
25
+    self.mode = 'text' unless mode.present?
26
+  end
19 27
 end

+ 0 - 1
app/views/agents/_form.html.erb

@@ -44,7 +44,6 @@
44 44
     </div>
45 45
   </div>
46 46
 
47
-
48 47
   <div class='event-related-region' data-can-create-events="<%= @agent.can_create_events? %>">
49 48
     <div class="control-group">
50 49
       <%= f.label :keep_events_for, "Keep events", :class => 'control-label' %>

+ 10 - 0
app/views/user_credentials/_form.html.erb

@@ -18,9 +18,17 @@
18 18
   </div>
19 19
 
20 20
   <div class="control-group">
21
+    <%= f.label :mode, :class => 'control-label' %>
22
+    <div class="controls">
23
+      <%= f.select :mode, options_for_select(UserCredential::MODES.map {|s| [s.classify, s] }, @user_credential.mode), {}, :class => 'span4' %>
24
+    </div>
25
+  </div>
26
+
27
+  <div class="control-group">
21 28
     <%= f.label :credential_value, :class => 'control-label' %>
22 29
     <div class="controls">
23 30
       <%= f.text_area :credential_value, :class => 'span8', :rows => 10 %>
31
+      <div id="ace-credential-value"></div>
24 32
     </div>
25 33
   </div>
26 34
 
@@ -28,3 +36,5 @@
28 36
     <%= f.submit "Save Credential", :class => "btn btn-primary" %>
29 37
   </div>
30 38
 <% end %>
39
+
40
+<%= javascript_include_tag "user_credentials" %>

+ 1 - 1
config/environments/production.rb

@@ -48,7 +48,7 @@ Huginn::Application.configure do
48 48
   end
49 49
 
50 50
   # Precompile additional assets (application.js.coffee.erb, application.css, and all non-JS/CSS are already added)
51
-  config.assets.precompile += %w( graphing.js )
51
+  config.assets.precompile += %w( graphing.js user_credentials.js )
52 52
 
53 53
   # Enable threaded mode
54 54
   # config.threadsafe!

+ 5 - 0
db/migrate/20140210062747_add_mode_to_user_credentials.rb

@@ -0,0 +1,5 @@
1
+class AddModeToUserCredentials < ActiveRecord::Migration
2
+  def change
3
+    add_column :user_credentials, :mode, :string, :default => 'text', :null => false
4
+  end
5
+end

+ 7 - 6
db/schema.rb

@@ -11,7 +11,7 @@
11 11
 #
12 12
 # It's strongly recommended to check this file into your version control system.
13 13
 
14
-ActiveRecord::Schema.define(:version => 20140127164931) do
14
+ActiveRecord::Schema.define(:version => 20140210062747) do
15 15
 
16 16
   create_table "agent_logs", :force => true do |t|
17 17
     t.integer  "agent_id",                                             :null => false
@@ -96,11 +96,12 @@ ActiveRecord::Schema.define(:version => 20140127164931) do
96 96
   add_index "links", ["source_id", "receiver_id"], :name => "index_links_on_source_id_and_receiver_id"
97 97
 
98 98
   create_table "user_credentials", :force => true do |t|
99
-    t.integer  "user_id",          :null => false
100
-    t.string   "credential_name",  :null => false
101
-    t.text     "credential_value", :null => false
102
-    t.datetime "created_at",       :null => false
103
-    t.datetime "updated_at",       :null => false
99
+    t.integer  "user_id",                              :null => false
100
+    t.string   "credential_name",                      :null => false
101
+    t.text     "credential_value",                     :null => false
102
+    t.datetime "created_at",                           :null => false
103
+    t.datetime "updated_at",                           :null => false
104
+    t.string   "mode",             :default => "text", :null => false
104 105
   end
105 106
 
106 107
   add_index "user_credentials", ["user_id", "credential_name"], :name => "index_user_credentials_on_user_id_and_credential_name", :unique => true

+ 4 - 0
spec/fixtures/user_credentials.yml

@@ -2,15 +2,19 @@ bob_aws_key:
2 2
   user: bob
3 3
   credential_name: aws_key
4 4
   credential_value: 2222222222-bob
5
+  mode: text
5 6
 bob_aws_secret:
6 7
   user: bob
7 8
   credential_name: aws_secret
8 9
   credential_value: 1111111111-bob
10
+  mode: text
9 11
 jane_aws_key:
10 12
   user: jane
11 13
   credential_name: aws_key
12 14
   credential_value: 2222222222-jane
15
+  mode: text
13 16
 jane_aws_secret:
14 17
   user: jane
15 18
   credential_name: aws_secret
16 19
   credential_value: 1111111111-jabe
20
+  mode: text